package cn.itcast.usermanage.service.impl;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.abel533.entity.Example;
import com.github.abel533.entity.Example.Criteria;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.itcast.usermanage.mapper.NewUserMapper;
import cn.itcast.usermanage.mapper.UserMapper;
import cn.itcast.usermanage.pojo.EasyUIResult;
import cn.itcast.usermanage.pojo.User;
import cn.itcast.usermanage.service.UserService;
@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private  NewUserMapper userMapper;
	
	@Override
	public EasyUIResult queryEasyUIResult(Integer pageNum, Integer pageSize) {
		// TODO Auto-generated method stub
		/*List<User> userList =this.userMapper.queryUsersByPage((pageNum-1)*pageSize,pageSize);		
		EasyUIResult easyUIResult = new EasyUIResult();
		easyUIResult.setTotal(15l);
		easyUIResult.setRows(userList);	
		return easyUIResult;*/
		
	    /*List<User> userList =this.userMapper.queryUsersByPage((pageNum-1)*pageSize,pageSize);*/		
		PageHelper.startPage(pageNum, pageSize);
//		List<User> userList = this.userMapper.queryUserAll();
		User user = new User();
		List<User> select = this.userMapper.select(user);
		PageInfo<User> pageInfo = new PageInfo<User>(select);
		
		EasyUIResult easyUIResult = new EasyUIResult();
		easyUIResult.setRows(pageInfo.getList());
		easyUIResult.setTotal(pageInfo.getTotal());	
		return easyUIResult;
		
	}

	@Override
	public List<User> queryUserAll() {
		//List<User> userList = this.userMapper.queryUserAll();
		return null;
	}

	@Override
	public void addUsers(User user1, User user2) throws Exception{
		//this.userMapper.addUser(user1);
		// 制造异常
		//int i=1/0;
		//this.userMapper.addUser(user2);
	}

	@Override
	public Boolean addUser(User user) throws Exception {
		int count = this.userMapper.insertSelective(user);
		if(count>0){
			return true;
		}
		return false;
	}

	@Override
	public Boolean deleteUser(String[] ids) {
		Example example =new Example(User.class);
		Criteria criteria = example.createCriteria();
		criteria.andIn("id", Arrays.asList((Object[])ids));
		int index =this.userMapper.deleteByExample(example);
		if(index>0){
			return true;
		}else{
			return false;
		}
		
	}

	@Override
	public EasyUIResult queryUserListByPage(Integer pageNum, Integer pageSize) {
		// TODO Auto-generated method stub
		PageHelper.startPage(pageNum, pageSize);
		  User user = new User();
		List<User> select = this.userMapper.select(user);
		PageInfo<User> pageInfo = new PageInfo<>(select);
		
		EasyUIResult easyUIResult = new EasyUIResult();
		easyUIResult.setRows(pageInfo.getList());
		easyUIResult.setTotal(pageInfo.getTotal());	
		return easyUIResult;
	}

	@Override
	public User queryUserById(Long id) {
		return  this.userMapper.selectByPrimaryKey(id);
	}

	@Override
	public Boolean saveUser(User user) {
	int updateByPrimaryKeySelective = this.userMapper.insert(user);
		if(updateByPrimaryKeySelective==1){
			return true;
		}
		return false;
	}

	@Override
	public int updateUser(User user) {
		 int updateByPrimaryKey = this.userMapper.updateByPrimaryKey(user);
		 
		return updateByPrimaryKey;
	}

	@Override
	public Integer deleteUser1(Long id) {
		int deleteByPrimaryKey = this.userMapper.deleteByPrimaryKey(id);
		return deleteByPrimaryKey;
	}

	@Override
	public void deleteUsers(List<Object> ids) {
Example example = new Example(User.class);
Criteria criteria = example.createCriteria();
criteria.andIn("id", ids);
int deleteByExample = this.userMapper.deleteByExample(example);
System.err.println(deleteByExample);
	}
	
//	@Override
//	public void deleteUsers(Long[] ids) {
//		Example example = new Example(User.class);
//		Criteria criteria = example.createCriteria();
//		criteria.andIn("id", ids);
//		int deleteByExample = this.userMapper.deleteByExample(example);
//		System.err.println(deleteByExample);
//	}
//	
	
	
}
